package LinkList

import node "go_algorithm/2_Linklist/Node"

type LinkList struct {
	Head   *node.Node
	Length int
}

// 初始化链表
func Init() *LinkList {
	return &LinkList{Head: nil, Length: 0}
}

// 添加元素到链表
func (ll *LinkList) Push(value int) {
	node := &node.Node{Value: value}
	if ll.Head == nil {
		ll.Head = node
	} else {
		cur := ll.Head
		for cur.Next != nil {
			cur = cur.Next
		}
		cur.Next = node
	}
	ll.Length++
}

// 删除元素
func (ll *LinkList) Delete(target int) bool {
	node := ll.Head
	if node == nil {
		return false
	}
	for node != nil {
		if node.Next.Value == target {
			node.Next = node.Next.Next
			ll.Length--
			return true
		}
		node = node.Next
	}
	return false
}

func (ll *LinkList) Print() {
	cur := ll.Head
	for cur != nil {
		println(cur.Value)
		cur = cur.Next
	}
}
